package com.bw.task.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bw.task.R;
import com.github.chrisbanes.photoview.PhotoView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ItemImg extends AppCompatActivity {

    private PhotoView photoimg;
    int num=0;
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_img);
        initView();
        Intent intent = getIntent();
        String img = intent.getStringExtra("img");
        Glide.with(this).load(img).into(photoimg);
        photoimg.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url = new URL(img);
                            HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
                            if (urlConnection.getResponseCode()==200){
                                InputStream inputStream = urlConnection.getInputStream();
                                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                                    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "img");
                                    LogUtils.i("-------------图片缓存地址："+file);
                                    if (!file.exists()){
                                        //连级存
                                        file.mkdirs();
                                    }
                                    Log.i("-----", "savaToSD: 存的路径"+file.getAbsolutePath());
                                    FileOutputStream fileOutputStream = new FileOutputStream(new File(file, num + ""));
                                    num++;
                                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);
                                    Log.i("randomcode","下载成功1 ");
                                    ToastUtils.showShort("下载成功");
                                    inputStream.close();
                                    fileOutputStream.close();
                                }
                            }

                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });


                return false;
            }
        });
    }

    private void initView() {
        photoimg = (PhotoView) findViewById(R.id.photoimg);
    }
}